package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.Para;
import com.xbai.goods.service.ParaService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jxbai
 * @desc
 * @date 2021/1/27
 */
@RestController
@RequestMapping("/para")
@CrossOrigin
@RequiredArgsConstructor
public class ParaController {

    private final ParaService paraService;

    @GetMapping("/category/{id}")
    public Result<List<Para>> findByCategoryId(@PathVariable("id") Integer categoryId) {
        List<Para> paraList = paraService.findByCategoryId(categoryId);
        return new Result<>(paraList);
    }

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) Para para, @PathVariable int page, @PathVariable int size) {
        //调用ParaService实现分页条件查询Para
        PageInfo<Para> pageInfo = paraService.findPage(para, page, size);
        return new Result<>(pageInfo);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用ParaService实现分页查询Para
        PageInfo<Para> pageInfo = paraService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    @PostMapping(value = "/search")
    public Result<List<Para>> findList(@RequestBody(required = false) Para para) {
        //调用ParaService实现条件查询Para
        List<Para> list = paraService.findList(para);
        return new Result<>(list);
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        //调用ParaService实现根据主键删除
        paraService.delete(id);
        return new Result();
    }

    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Para para, @PathVariable Integer id) {
        //设置主键值
        para.setId(id);
        //调用ParaService实现修改Para
        paraService.update(para);
        return new Result();
    }

    @PostMapping
    public Result add(@RequestBody Para para) {
        //调用ParaService实现添加Para
        paraService.add(para);
        return new Result();
    }

    @GetMapping("/{id}")
    public Result<Para> findById(@PathVariable Integer id) {
        //调用ParaService实现根据主键查询Para
        Para para = paraService.findById(id);
        return new Result<>(para);
    }

    @GetMapping
    public Result<List<Para>> findAll() {
        //调用ParaService实现查询所有Para
        List<Para> list = paraService.findAll();
        return new Result<>(list);
    }
}
